<?php

namespace Zf2User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zf2User\Form\SignupForm as SignupForm;

class AuthController extends AbstractActionController
{

    public function indexAction()
    {
        return new ViewModel();
    }

    public function registerAction()
    {
        $form = new SignupForm('register_user' , array('em' => NULL));
       
        $request = $this->getRequest();
        if($request->isPost())
        {
            $form->setData($request->getPost());
            if($form->isValid())
            {
                try {
                    $service = $this->getServiceLocator()->get('Zf2User\Service\User');
                    $post = $request->getPost()->toArray();//die(print_r($post));
                    $post['role'] = 2; // Role Client

                    if ($service->persist($post))
                    {
                        $this->flashMessenger()->addMessage('Luu thanh cong');
                    }

                } catch (\Exception $e) { 
                    $this->flashMessenger()->addMessage('Luu that bai!');
                }

                //return $this->redirect()->toRoute('user-auth');
            }
            
        }
        
        return new ViewModel(array(
            'form' => $form 
        ));
    }


}

